package pp.infrastructure.threadpool;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

@Configuration
public class GenericCommonTaskThreadPoolConfig {

    @Bean("genericCommonTaskThreadPool")
    public ThreadPoolExecutor taskExecutor() {
        int corePoolSize = 5; // 核心线程数
        int maximumPoolSize = 10; // 最大线程数
        long keepAliveTime = 30L; // 空闲线程存活时间
        TimeUnit unit = TimeUnit.SECONDS; // 时间单位
        int queueCapacity = 10; // 工作队列容量
        BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(queueCapacity); // 工作队列
        RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy(); // 拒绝策略

        return new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                workQueue,
                handler
        );
    }
}
